package com.emato.cus.util;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SendMsgUtil {

    // 编码格式。发送编码格式统一用UTF-8
    private static String ENCODING = "UTF-8";

    // 请求地址
    private static String SMS_URL = "https://sms.yunpian.com/v2/sms/batch_send.json";

    // cps报警通知 apikey
    private static String API_KEY = "2e5c9d259b92e842c8d6b6a7e30dda4f";

    // 多人手机号码配置，用英文,分隔
//    private static String MOBILE = "15290013963,17875264004";
    private static String MOBILE = "17727992046";

    /**
     * 发送短信消息
     * 方法说明
     * @Discription:扩展说明
     * @param text
     * @return
     * @return String
     */
    @SuppressWarnings("deprecation")
    public static String sendMsg(String mobile, String text) {
        System.out.println(text);
        if (API_KEY != null && SMS_URL != null) {
            Map<String, String> params = new HashMap<String, String>();
            params.put("apikey", API_KEY);
            //手机号码，多个号码使用英文逗号进行分割
            params.put("mobile", mobile);
            //将短信内容进行URLEncoder编码
            params.put("text", text);

            return post(SMS_URL, params);
        } else {
            return "";
        }
    }

    /**
     * 随机生成6位随机验证码
     * 方法说明
     * @return
     * @return String
     */
    public static String createRandomVcode() {
        //验证码
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int) (Math.random() * 9);
        }
        return vcode;
    }

    /**
     * 基于HttpClient 4.3的通用POST方法
     *
     * @param url       提交的URL
     * @param paramsMap 提交<参数，值>Map
     * @return 提交响应
     */
    public static String post(String url, Map<String, String> paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity, ENCODING);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return responseText;
    }

    /**
     * 测试
     * 方法说明
     * @param args
     * @return void
     */
    public static void main(String[] args) {
        System.out.println(sendMsg(MOBILE ,"【前海电商】读取操作员卡程序心跳异常，时间：2020-06-30。"));
    }
}